package com.langchain4j.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description:
 * @Author: linjun
 * @Date: 2025/9/15 09:51
 */
@Configuration
public class ThreadPoolConfig {

    @Bean("AsyncPushLogExecutor")
    public ThreadPoolTaskExecutor bidConfirmAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 处理器的核心数
        int coreNum = Runtime.getRuntime().availableProcessors() * 2;
        // IO密集
        // 设置最大线程池线程数量为核心线程池的两倍
        int maxPoolSize = coreNum * 2;
        taskExecutor.setCorePoolSize(coreNum);
        taskExecutor.setMaxPoolSize(maxPoolSize);
        taskExecutor.setQueueCapacity(maxPoolSize);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.setThreadNamePrefix("AsyncPushLogExecutor-");
        taskExecutor.initialize();
        return taskExecutor;
    }
}
